{% extends 'sales/base.html' %}
{% load static %}
{% load paginate %}
{% load thumbnail %}
{% block extralinks %}
<style>
  .form-group label{
    font-weight: 800;
  }
</style>
{% endblock %}
{% block content %}

<div class="main_container">
  
  <div class="row marl">
    <div class="col-lg-12 text-right">
      <span class="d-inline"><a class="primary_btn" href="{% url 'common:create_doc' %}"><i class="fa fa-plus"></i> Add
          New Document</a></span>
    </div>
  </div>
  <div class="filter_row list_filter_row row marl">
    <div class="col-md-12">
      <div class="card">
        <div class="card-body">
          <form id="docs_filter" method="POST" action="">
            <div class="card-body">
              <div class="card-title">Filters</div>
              <div class="row marl">
                <div class="filter_col col-md-4">
                  <div class="form-group">
                    <label for="exampleInputEmail1">Title</label>
                    <input type="text" class="form-control" placeholder="Document Title" name="doc_name"
                      {% if request.POST.doc_name %} value="{{request.POST.doc_name}}" {%endif%} />
                  </div>
                </div>
                <div class="filter_col col-md-2">
                  <div class="form-group">
                    <label for="exampleInputEmail1">Status</label>
                    <select class="form-control" id="id_status" name="status">
                      <option value="">--Status--</option>
                      {% for each_status in status_choices %}
                      <option value="{{each_status.0}}" {% if request.POST.status %}
                        {% ifequal each_status.0 request.POST.status %}selected{% endifequal %}{% endif %}>
                        {{each_status.1}} </option>
                      {% endfor%}
                    </select>
                  </div>
                </div>
                <div class="filter_col col-md-3">
                  <div class="form-group">
                    <label for="exampleInputEmail1">Shared To</label>
                    <select class="assigned_users form-control" name="shared_to" multiple="multiple">
                      {% for user in users %}
                      <option value="{{user.id}}" {% if user.id in sharedto_list %} selected="" {% endif %}>
                        {{user.email}}</option>
                      {% endfor %}
                    </select>
                  </div>
                </div>
                <div class="filter_col col-3">
                  <div class="form-group buttons_row">
                    <button class="btn btn-primary save" type="submit">Search</button>
                    <a href="{% url 'common:doc_list' %}" class="btn btn-default clear">Clear</a>
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
  
  <div class="table_container_row row marl ">
    <div class="col-md-12">
      <div class="card">
        <div class="card-body">
          <div class="panel-heading-list card-title text-right">
            <span class="total_count float-left">Documents - {% if show_pageitems %} {% show_pageitems %}{% else %}
              {{documents|length}}{% endif %}</span>
            <span class="filter_toggle ">
              <a href="#" class="primary_btn"><i class="fas fa-filter"></i></a>
            </span>
          </div>
          <div class="table-responsive">
            <table class="table ">
              <thead>
                {% if documents|length > 0 %}
                <tr>
                  <th width="5%">S.No</th>
                  <th width="15%">Title</th>
                  <th width="5%">Created By</th>
                  <th width="15%">Created On</th>
                  <th width="5%">Status</th>
                  <th width="20%">Shared To</th>
                  <th width="10%">Actions</th>
                </tr>
                {% endif %}
              </thead>
              <tbody>
                {% if per_page %}
                {% paginate per_page documents %}
                {% else%}
                {% paginate 10 documents %}
                {% endif %}
                {% for document in documents %}
                <tr id="account_details{{each_account.id}}" class="text-center">
                  <td scope="row">{{ forloop.counter }}</td>
                  <td>{% if document.title %}<a data-toggle="modal"
                      href="#" data-target="#exampleModalCenter_doc{{document.id}}">{{ document.title }}</a>{% endif %}</td>
                  <td>
                    {% if document.created_by %}
                    {% if document.created_by.profile_pic %}
                    {% thumbnail document.created_by.profile_pic "40x40" crop="center" as im %}
                    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"
                      title="{{ document.created_by }}">
                    {% endthumbnail %}
                    {% else %}
                    <img src="{% static 'images/user.png' %}" title="{{ document.created_by }}" width="40" height="40">
                    {% endif %}
                    {% else %}
                    None
                    {% endif %}
                  </td>
                  <td title="{{document.created_on}}">{{document.created_on_arrow}}</td>
                  <td>{{document.status}}</td>
                  <td>
                    {% with document_users=document.shared_to.all %}
                    {% for user in document_users %}
                    {% if user.profile_pic %}
                    {% thumbnail user.profile_pic "40x40" crop="center" as im %}
                    <a href="{% url 'common:view_user' user.id %}">
                      <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" title="{{ user.email }}">
                    </a>
                    {% endthumbnail %}
                    {% else %}
                    <a href="{% url 'common:view_user' user.id %}">
                      <img src="{% static 'images/user.png' %}" title="{{ user.email }}" width="40" height="40">
                    </a>
                    {% endif %}
                    {% empty %}
                    None
                    {% endfor %}
                    {% endwith %}
                  </td>
                  <td class="actions">
                    {% if request.user == document.created_by or request.user.role == 'ADMIN' or request.user.has_sales_access %}
                    <a href="{% url 'common:view_doc' document.id %}" class="btn btn-info edit"><i
                      class="fas fa-eye"></i></a>
                    <a href="{% url 'common:edit_doc' document.id %}" class="btn btn-success edit"><i
                        class="fas fa-pencil-alt"></i></a>
                    <a href="{% url 'common:remove_doc' document.id %}" class="btn btn-danger delete remove_document"><i
                        class="fas fa-trash-alt"></i></a>
                    {% endif %}
                    {% if request.user in document.shared_to.all or request.user == document.created_by or request.user.role == 'ADMIN' %}
                    <a href="{% url 'common:download_document' document.id %}" class="btn btn-info"><i
                        class="fas fa-download"></i></a>
                    {% endif %}
                  </td>
                </tr>
                {% endfor %}
              </tbody>
            </table>
          </div>
          {% ifequal documents|length 0 %}
          <h6 class="text-center">No Documents Found</h6>
          {% endifequal %}
          <div class="text-center row marl">
            {% show_pages %}
          </div>
        </div>
      </div>
      <br clear="all">
    </div>
  </div>
  
  
</div>



{% for doc_obj in documents %}

<div class="modal fade" id="exampleModalCenter_doc{{doc_obj.id}}" tabindex="-1" role="dialog"
  aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">{{ doc_obj.title }}</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        
        
        <div class="" id="">
          <div class="">
            <div class="col-md-12">
              <div class="card">
                <div class="card-body" id="datashow">
                  <div class="panel-heading card-title view-pad text-center">
                    
                    
                  </div>
                  <div class="row marl no-gutters">
                    <div class="col-md-8">
                      {% if doc_obj.title %}
                      <div class="filter_col col-md-12">
                        <div class="form-group">
                          <label class="contact_field_label" for="id_title" data-name="title">Title</label>
                          <div class="contact_field" id="doc_title" data-name="name">{{ doc_obj.title }} </div>
                        </div>
                      </div>
                      {% endif %}
                      {% if doc_obj.status %}
                      <div class="filter_col col-md-12">
                        <div class="form-group">
                          <label class="contact_field_label" for="id_status" data-name="status">Status</label>
                          <div class="contact_field" id="doc_status" data-name="status">{{ doc_obj.status }} </div>
                        </div>
                      </div>
                      {% endif %}
                      <div class="filter_col col-md-12">
                        <div class="form-group">
                          <label class="contact_field_label" for="id_created_by" data-name="created_by">Created
                            By</label>
                          <div class="contact_field" id="created_by" data-name="created_by">{{ doc_obj.created_by }}
                          </div>
                        </div>
                      </div>
                      <div class="filter_col col-md-12">
                        <div class="form-group">
                          <label class="contact_field_label" for="id_created_on" data-name="created_on">Created
                            On</label>
                          <div class="contact_field" id="created_on" data-name="created_on">{{ doc_obj.created_on }}
                          </div>
                        </div>
                      </div>
                      {% with document_users=document.shared_to.all %}
                      {% if document_users %}
                      <div class="filter_col col-md-12">
                        <div class="form-group">
                          <label class="contact_field_label" for="id_created_on" data-name="created_on">Shared
                            To</label>
                          <br>
                          {% for user in document_users %}
                          {% if user.profile_pic %}
                          {% thumbnail user.profile_pic "40x40" crop="center" as im %}
                          <span>
                            <a href="{% url 'common:view_user' user.id %}">
                              <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"
                                title="{{ user.email }}">
                            </a>
                          </span>
                          {% endthumbnail %}
                          {% else %}
                          <span>
                            <a href="{% url 'common:view_user' user.id %}">
                              <img src="{% static 'images/user.png' %}" title="{{ user.email }}" width="40" height="40">
                            </a>
                          </span>
                          {% endif %}
                          {% empty %}
                          None
                          {% endfor %}
                        </div>
                      </div>
                      {% endif %}
                      {% endwith %}
                    </div>
                    <div class="col-md-4">
                      <div class="profile_pic">
                        {% if doc_obj.document_file %}
                        <i class="{{file_type_code}}" aria-hidden="true" style="font-size:3rem;"></i>
                        
                        
                        </td>
                        {% endif %}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    
  </div>
</div>


{% endfor %}




{% endblock %}
{% block js_block %}
<script type="text/javascript">
  $(document).ready(function () {
    $('.assigned_users').select2();
    $(".filter_toggle").click(function () {
      $(".list_filter_row").toggle();
    });
  });

  search = "{{search}}"

  if (search == 'True') {
    $(".list_filter_row").show();
  }

  $('.remove_document').click(function (e) {
    e.preventDefault()
    url = $(this).attr('href')
    if (!confirm('Are you sure you want to delete?'))
      return;
    window.location = $(this).attr('href')
  });

  $(".reset").click(function (e) {
    window.location = "{% url 'common:doc_list'%}"
  });

  $("a[rel='page']").click(function (e) {
    e.preventDefault();
    $('#docs_filter').attr("action", $(this).attr("href"));
    $('#docs_filter').submit();
  });
</script>
{% endblock js_block %}